﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.DwzFrame;
using NHibernate;
using NHibernate.Criterion;
using ZLCMS.Business;
using ZLCMS.Data.Model;

namespace ZLCMS.Web.SystemManage.Basics.Kind
{
    public partial class List : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new FrameHelper(this) { RecordCount = GetRecordCount() };

            if (_framePage.IsAjaxPagerPostBack)
            {
                BindRepList(_framePage.PageIndex, _framePage.PageSize);
            }
            else
            {
                BindRepList(1, 20);
            }
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindRepList(int pageIndex, int pageSize)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<CmsKindInfo>();

                criteria.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize);

                repList.DataSource = criteria.List();
                repList.DataBind();
            }
        }


        protected int GetRecordCount()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<CmsKindInfo>();
                return (int)criteria.SetProjection(Projections.RowCount()).UniqueResult();
            }
        }
    }
}
